/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.housekeeper.config;

import feign.RequestInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.http.HttpHeaders.REFERER;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.HttpHeaders.ORIGIN;
import static org.springframework.http.HttpHeaders.HOST;

/**
 * feign请求头配置
 *
 * @author y84241604
 * @since 2022-07-11
 */
@Configuration
public class FeignHeadConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return Optional.of((RequestInterceptor) requestTemplate -> {
            ServletRequestAttributes requestAttributes
                = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            requestTemplate.header(REFERER, request.getHeader(REFERER));
            requestTemplate.header(AUTHORIZATION, request.getHeader(AUTHORIZATION));
            requestTemplate.header(ORIGIN, request.getHeader(ORIGIN));
            requestTemplate.header(HOST, request.getHeader(HOST));
        }).get();
    }
}
